package com.snail.cloud.util;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Random;

/**
 * @author MR.LHL
 * @date 2024/2/29
 */

@Component
public class SendSmsUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(SendSmsUtil.class);
    private static String appKey;
    private static String appSecret;
    private static String smsTemplateCode;
    private static String smsFreeSignName;

    private static final int CODE_LENGTH = 6; // 验证码长度
    private static final String CODE_CHARS = "0123456789"; // 验证码字符集

    public static final String PREFIX_CODE = "snail:";

    public static String generateVerificationCode() {
        StringBuilder code = new StringBuilder();
        Random random = new Random();

        for (int i = 0; i < CODE_LENGTH; i++) {
            int index = random.nextInt(CODE_CHARS.length());
            code.append(CODE_CHARS.charAt(index));
        }
        return code.toString();
    }

    public static boolean sendSms(String code, String mobileNumber) {
        DefaultProfile profile = DefaultProfile.getProfile("default", appKey, appSecret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("PhoneNumbers", mobileNumber);
        request.putQueryParameter("SignName", smsFreeSignName);
        request.putQueryParameter("TemplateCode", smsTemplateCode);
        request.putQueryParameter("TemplateParam", "{\"code\":\"" + code + "\"}");
        CommonResponse response = null;
        try {
            response = client.getCommonResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return checkCommonResponse(response);
    }

    private static boolean checkCommonResponse(CommonResponse response) {
        if (response == null) {
            return false;
        }
        String data = response.getData();
        if (data == null) {
            return false;
        }
        LOGGER.info("======>发送短信完成，返回数据：{}", data);
        JsonObject dataObject = (JsonObject) JsonParser.parseString(data);
        String code = dataObject.get("Code").getAsString();
        return "OK".equals(code);
    }

    @Value("${aliyun.smsTemplateCode}")
    public void setSmsTemplateCode(String smsTemplateCode) {
        SendSmsUtil.smsTemplateCode = smsTemplateCode;
    }

    @Value("${aliyun.smsFreeSignName}")
    public void setSmsFreeSignName(String smsFreeSignName) {
        SendSmsUtil.smsFreeSignName = smsFreeSignName;
    }

    @Value("${aliyun.appKey}")
    public void setAppKey(String appKey) {
        SendSmsUtil.appKey = appKey;
    }

    @Value("${aliyun.appSecret}")
    public void setAppSecret(String appSecret) {
        SendSmsUtil.appSecret = appSecret;
    }


}
